//package top.tenebrous.yzl.handler.authority;
//
//import lombok.extern.slf4j.Slf4j;
//import top.tenebrous.yzl.auth.AuthTokenGenerator;
//import top.tenebrous.yzl.basic.constant.Constants;
//import top.tenebrous.yzl.chain.AbstractJesticChainHandler;
//import top.tenebrous.yzl.context.JesticContext;
//import top.tenebrous.yzl.context.JesticTask;
//import top.tenebrous.yzl.context.Response;
//import top.tenebrous.yzl.entity.JesticServer;
//import top.tenebrous.yzl.enums.FailCodeEnum;
//import top.tenebrous.yzl.instance.AbstractInstanceContext;
//import top.tenebrous.yzl.instance.Instance;
//import top.tenebrous.yzl.instance.ServerInstance;
//import top.tenebrous.yzl.model.request.JesticAuthorityRequest;
//import top.tenebrous.yzl.model.response.JesticAuthorityResponse;
//import top.tenebrous.yzl.service.IAuthorityService;
//
//import java.util.Objects;
//
///**
// * @author yazhang6
// */
//@Slf4j
//public class AuthorityHandler extends AbstractJesticChainHandler<JesticAuthorityRequest, JesticAuthorityResponse> {
//
//    private final IAuthorityService authorityService;
//
//    private final AbstractInstanceContext abstractInstanceContext;
//
//    private final AuthTokenGenerator authTokenGenerator;
//
//    public AuthorityHandler(IAuthorityService authorityService, AbstractInstanceContext abstractInstanceContext, AuthTokenGenerator authTokenGenerator) {
//        this.authorityService = authorityService;
//        this.abstractInstanceContext = abstractInstanceContext;
//        this.authTokenGenerator = authTokenGenerator;
//    }
//
//    private int order;
//
//    @Override
//    public void handler(JesticContext<? extends JesticTask, ? extends Response> context) {
//        // 从上下文中获取请求信息
//        JesticAuthorityRequest request = (JesticAuthorityRequest) context.getRequest();
//        log.info("JesticAuthHandler.handler.request[{}]", request);
//        request.start();
//        // 从上下文中获取响应信息
//        JesticAuthorityResponse response = (JesticAuthorityResponse) context.getResponse();
//        // 获取服务信息
//        JesticServer server = authorityService.queryServer(request.getServerId(), request.getServerName());
//
//        if (Objects.isNull(server)) {
//            log.info("jestic [ServerServiceGrpcImpl.authority] client [{}] server not found", request.getServerName());
//            response.setCode(FailCodeEnum.SERVER_NOT_FOUND.getCode());
//            response.setMessage("server not found");
//        } else if (server.getServerStatus() != Constants.INT_ONE) {
//            log.info("jestic [ServerServiceGrpcImpl.authority] client [{}] status  not abnormal", request.getServerName());
//            response.setCode(FailCodeEnum.SERVER_NOT_RUNNING.getCode());
//            response.setMessage("server status not is running");
//        } else {
//            Instance instance = new ServerInstance(abstractInstanceContext.initContext(server, authTokenGenerator)).initInstance(request);
//            log.info("jestic [ServerServiceGrpcImpl.authority] client [{}] init instance:[{}]", request.getServerName(), instance.getInstanceId());
//            // 给与响应
//            buildResponse(request, response, instance);
//        }
//        if (null != nextHandler) {
//            nextHandler.handler(context);
//        } else {
//            request.end();
//        }
//    }
//
//    private void buildResponse(JesticAuthorityRequest request, JesticAuthorityResponse response, Instance instance) {
//        response.setId(request.getServerId());
//        response.setServerId(request.getServerId());
//        response.setInstanceId(instance.getInstanceId());
//        response.setInstanceStatus(instance.getInstanceStatus().getCode());
//        response.setServerName(instance.getServerName());
//        response.setIsAuth(Boolean.TRUE);
//        response.setHealth(instance.getInstanceStatus().getCode());
//        response.setExpire(instance.getExpireTime());
//        response.setToken(instance.getToken());
//        response.setCode(Constants.SUCCESS);
////        response.setMessage(RetCode.SUCCESS.name());
//    }
//
//    @Override
//    public int order(String taskType) {
//        return this.order;
//    }
//
//    @Override
//    public int getOrder() {
//        return order;
//    }
//
//    @Override
//    public AbstractJesticChainHandler<JesticAuthorityRequest, JesticAuthorityResponse> setOrder(int order) {
//        this.order = order;
//        return this;
//    }
//}
